package com.example.juc.threadwaitornotify;

import java.util.concurrent.TimeUnit;

public class demo01 {
    public static void main(String[] args) throws InterruptedException {

        Object objectlock=new Object();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {

                synchronized (objectlock){
                    System.out.println(Thread.currentThread().getName()+"开始执行");

                    try {
                        objectlock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName()+"被唤醒");
                }
            }
        },"线程1");
        thread.start();

        TimeUnit.SECONDS.sleep(1);
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (objectlock){
                    System.out.println(Thread.currentThread().getName()+"开始执行");
                    objectlock.notify();
                }
            }
        },"线程2");
        thread2.start();
    }
}
